local ByteArray = require("framework.cc.utils.ByteArray");
local Packet = class("Packet",ByteArray)

--整个数据包长度占用字节数
local DATA_LEN = 2  --2,4 默认是2个字节

--数据存取方式 小端or大端
local DATA_ENDIAN= 1  --0小端1大端


--协议格式  长度 + 协议号 + 数据

--  uInt32  数据长度  4字节
--  uInt16  协议ID    2字节
--  数据

--[[
    local ByteArray = require("framework.cc.utils.ByteArray");
    local ba = nil;
    local bb = nil;
    local old = false   
    if old then
        -- body
        ba = ByteArray.new();
        ba:setPos(1);
        ba:writeByte(0x59)
        ba:writeInt(11)
        ba:writeShort(1101)
        ba:writeStringUShort("")
        ba:writeStringUShort("中文")
    else
        ba = Packet.new();
        ba:setPos(5);
        ba:pWriteUint8(0x59);
        ba:pWriteInt32(11);
        ba:pWriteInt16(1101);
        ba:writeStringUShort("")
        ba:writeStringUShort("中文")
    end
	print(ba:toString());
    print("11111111111111111111111111111111");
    if old then
        -- body
        bb = ByteArray.new();
        bb:writeBytes(ba);
        bb:setPos(1);
        print(bb:readByte());
        print(bb:readInt());
        print(bb:readShort());
        print(bb:readStringUShort());
        print(bb:readStringUShort());
    else
        print("xxxxxxxxxxxxxxxxxx");
        bb = Packet.new();
        bb:writeBytes(ba);
        bb:setPos(5);
        print(bb:pReadUint8());
        print(bb:pReadInt32());
        print(bb:pReadInt16());
        print(bb:readStringUShort());
        print(bb:readStringUShort());
    end
]]




function Packet:ctor()
    -- body
    local endian = ByteArray.ENDIAN_LITTLE;
    if DATA_ENDIAN == 1 then
        endian = ByteArray.ENDIAN_BIG;
    end
    Packet.super.ctor(self,endian);
end

function Packet:initWrite()
    -- body
    self.length = 0;
    self:setPos(DATA_LEN + 1);
end

------------------------------------------------------------------Write-------------------------------------------------------------------
function Packet:addDataLength(length)
    -- body
    self.length = self.length + length;
end

function Packet:pWriteChar(data)
    -- body
    local oldPos = self:getPos();
    self:writeChar(data);
    local newPos = self:getPos();
    self:addDataLength(newPos - oldPos);
end

function Packet:pWriteUint8(data)
    -- body
    local oldPos = self:getPos();
    self:writeUByte(data);
    local newPos = self:getPos();
    self:addDataLength(newPos - oldPos);
end

function Packet:pWriteInt8(data)
    -- body
    local oldPos = self:getPos();
    self:writeByte(data);
    local newPos = self:getPos();
    self:addDataLength(newPos - oldPos);
end

function Packet:pWriteUint16(data)
    -- body
    local oldPos = self:getPos();
    self:writeUShort(data);
    local newPos = self:getPos();
    self:addDataLength(newPos - oldPos);
end

function Packet:pWriteInt16(data)
    -- body
    local oldPos = self:getPos();
    self:writeShort(data);
    local newPos = self:getPos();
    self:addDataLength(newPos - oldPos);
end

function Packet:pWriteUint32(data)
    -- body
    local oldPos = self:getPos();
    self:writeUInt(data);
    local newPos = self:getPos();
    self:addDataLength(newPos - oldPos);
end

function Packet:pWriteInt32(data)
    -- body
    local oldPos = self:getPos();
    self:writeInt(data);
    local newPos = self:getPos();
    self:addDataLength(newPos - oldPos);
end

function Packet:pWriteUint64(data)
    data = '' .. data
    self:pWriteShortString(data)
end

function Packet:pWriteInt64(data)
    data = '' .. data
    self:pWriteShortString(data)
end


function Packet:pWriteFloat(data)
    -- body
    local oldPos = self:getPos();
    self:writeFloat(data);
    local newPos = self:getPos();
    self:addDataLength(newPos - oldPos);
end

function Packet:pWriteDouble(data)
    -- body
    local oldPos = self:getPos();
    self:writeDouble(data);
    local newPos = self:getPos();
    self:addDataLength(newPos - oldPos);
end

function Packet:pWriteLittleString(data)
    -- body
    self:pWriteString(data,1)
end

function Packet:pWriteShortString(data)
    -- body
    self:pWriteString(data,2)
end

function Packet:pWriteLongString(data)
    -- body
    self:pWriteString(data,4)
end

function Packet:pWriteString(data,strLen)
    -- body
    strLen = strLen or 2
    local dataLength = string.len(data);
    if strLen == 1 then
        self:pWriteUint8(dataLength);
    elseif strLen == 2 then
        self:pWriteUint16(dataLength);
    elseif strLen == 4 then
        self:pWriteUint32(dataLength);
    else
        self:pWriteUint8(dataLength);
    end
    local oldPos = self:getPos();
    self:writeString(data);
    local newPos = self:getPos();
    self:addDataLength(newPos - oldPos);
end

function Packet:pWriteCharArray(data)
    -- body
    local oldPos = self:getPos();
    self:writeStringBytes(data);
    local newPos = self:getPos();
    self:addDataLength(newPos - oldPos);
end

function Packet:pWriteBool(data)
    -- body
    local oldPos = self:getPos();
    self:writeBool(data);
    local newPos = self:getPos();
    self:addDataLength(newPos - oldPos);
end

function Packet:pWriteBuf(data)
    -- body
    self:writeBuf(data);
end

function Packet:pWriteEnd()
    self:setPos(1);
    if DATA_LEN == 2 then
        self:writeUShort(self.length);
    elseif DATA_LEN == 4 then
        self:writeUInt(self.length);
    else
        self:writeUShort(self.length);
    end
    str,len = self:toString()
    mgrs.log:printDebug("writeEnd str=%s len=%d",str,len);
    mgrs.log:printDebug("self.length = %d",self.length)
end


---------------------------------------------------------------------Read-------------------------------------------------------------
function Packet:pReadLength()
    -- body
    if DATA_LEN == 2 then
        return self:pReadUint16()
    elseif DATA_LEN == 4 then
        return self:pReadUint32()
    end
    return self:pReadUint16();
end

function Packet:pReadProtoId()
    -- body
    return self:pReadUint16();
end

function Packet:pReadChar()
    -- body
    return self:readChar();
end

function Packet:pReadUint8()
    -- body
    return self:readUByte();
end

function Packet:pReadInt8()
    -- body
    return self:readByte()
end

function Packet:pReadUint16()
    -- body
    return self:readUShort();
end

function Packet:pReadInt16()
    -- body
    return self:readShort();
end

function Packet:pReadUint32()
    -- body
    return self:readUInt();
end

function Packet:pReadInt32()
    -- body
    return self:readInt();
end

function Packet:pReadUint64()
    s = self:pReadShortString()
    return tonumber(s)
end

function Packet:pReadInt64()
    s = self:pReadShortString()
    return tonumber(s)
end

function Packet:pReadFloat()
    -- body
    return self:readFloat();
end

function Packet:pReadDouble()
    -- body
    return self:readDouble();
end

function Packet:pReadLittleString()
    -- body
    return self:pReadString(1)
end

function Packet:pReadShortString()
    -- body
    return self:pReadString(2)
end

function Packet:pReadLongString()
    -- body
    return self:pReadString(4)
end

function Packet:pReadString(strLen)
    -- body
    strLen = strLen or 2
    local length = 0
    if strLen == 1 then
        length = self:pReadUint8();
    elseif strLen == 2 then
        length = self:pReadUint16();
    elseif strLen == 4 then
        length = self:pReadUint32();
    end
    return self:readStringBytes(length);
end

function Packet:pReadCharArray()
    -- body
    return {};
end

function Packet:pReadBool()
    -- body
    return self:readBool();
end

function Packet:pReadBuf()
    -- body
    return {};
end



return Packet;
